<template>
  <div>
    <a-input-search style="margin-bottom: 8px" placeholder="Search" @change="onChange" />
    <a-tree @select="onSelect" :tree-data="treeData"  default-expand-all :default-selected-keys="['0-0-0']"></a-tree>
  </div>
</template>

<script>
import { getSciencesOrNot } from '@/api/science'
const x = 3;
const y = 2;
const z = 1;
const gData = [];

const generateData = (_level, _preKey, _tns) => {
  const preKey = _preKey || '0';
  const tns = _tns || gData;

  const children = [];
  for (let i = 0; i < x; i++) {
    const key = `${preKey}-${i}`;
    tns.push({ title: key, key, scopedSlots: { title: 'title' } });
    if (i < y) {
      children.push(key);
    }
  }
  if (_level < 0) {
    return tns;
  }
  const level = _level - 1;
  children.forEach((key, index) => {
    tns[index].children = [];
    return generateData(level, key, tns[index].children);
  });
};  
const treeData = [
  {
    title: '物资公司招标单位',
    key: '0-0',
    id: '53085e2c9f1f4685801ef31994f57c49',
    children: [
      { title: '招标', key: '0-0-0', id: '59ced46f24184856997762478cc5b39a'},
      { title: '招标1', key: '0-0-1', id: '59ced46f24184856997762478cc5b39b'},
      { title: '招标2', key: '0-0-3', id: '59ced46f24184856997762478cc5b39c'},
    ],
  },
];
generateData(z);

const dataList = [];
const generateList = data => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i];
    const key = node.key;
    dataList.push({ key, title: key });
    if (node.children) {
      generateList(node.children);
    }
  }
};
generateList(gData);

const getParentKey = (key, tree) => {
  let parentKey;
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];
    if (node.children) {
      if (node.children.some(item => item.key === key)) {
        parentKey = node.key;
      } else if (getParentKey(key, node.children)) {
        parentKey = getParentKey(key, node.children);
      }
    }
  }
  return parentKey;
};
export default {
  name:'a'+ Date.now(),
  data() {
    return {
      expandedKeys: [],
      searchValue: '',
      autoExpandParent: true,
      gData,
      treeData,
      
    };
  },
  methods: {
    onExpand(expandedKeys) {
      this.expandedKeys = expandedKeys;
      this.autoExpandParent = false;
    },
    onSelect(selectedKeys, info) {
    console.log(info.selectedNodes[0].data.props.id)
    let id = info.selectedNodes[0].data.props.id
    getSciencesOrNot({deptId:id,type:1}).then((res)=>{
        console.log("不是科技人员："+res.data)
        this.onLoadDatas = res.data
    })
    },
    onChange(e) {
      const value = e.target.value;
      const expandedKeys = dataList
        .map(item => {
          if (item.title.indexOf(value) > -1) {
            return getParentKey(item.key, gData);
          }
          return null;
        })
        .filter((item, i, self) => item && self.indexOf(item) === i);
      Object.assign(this, {
        expandedKeys,
        searchValue: value,
        autoExpandParent: true,
      });
    },
  },
};
</script>